/**
 * Created by wqz806687102 on 2016/1/11.
 */
'use strict';

angular.module('Query').controller('PostsRecordController', ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication', 'PublicEnum',
    function($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication, PublicEnum) {
        $scope.serachText; //关键字
        $scope.week = true;
        $scope.month = false;
        $scope.alltime = false;
        //复选框事件
        $scope.chbChangeWeek = function() {//周查询
            if ($scope.week) {
                $scope.month = false;
                $scope.alltime = false;
                $scope.$digest();

                if ($scope.serachText == undefined || $scope.serachText.trim() == "") {
                    return;
                };
                //本周起止时间
                var weekStartDate = calCurrentWeekTime(true) + ' 00:00:00';
                var weekEndDate = calCurrentWeekTime(false) + ' 23:59:59';
                query(PublicFunction.stringToDateTime(weekStartDate), PublicFunction.stringToDateTime(weekEndDate));
            };
        };
        $scope.chbChangeMonth = function() {//月查询
            if ($scope.month) {
                $scope.week = false;
                $scope.alltime = false;
                $scope.$digest();

                if ($scope.serachText == undefined || $scope.serachText.trim() == "") {
                    return;
                };
                //本月起止时间
                var monthStartDate = calCurrentMonthTime(true) + ' 00:00:00';
                var monthEndDate = calCurrentMonthTime(false) + ' 23:59:59';
                query(PublicFunction.stringToDateTime(monthStartDate), PublicFunction.stringToDateTime(monthEndDate));
            };
        };
        $scope.chbChangeAll = function() {//全部查询
            if ($scope.alltime) {
                $scope.week = false;
                $scope.month = false;
                $scope.$digest();

                if ($scope.serachText == undefined || $scope.serachText.trim() == "") {
                    return;
                };
                query();
            };
        };

        //查询
        $scope.btnQuery = function() {
            if ($scope.serachText == undefined || $scope.serachText.trim() == "") {
                return;
            };
            if ($scope.week) {
                var weekStartDate = calCurrentWeekTime(true) + ' 00:00:00';
                var weekEndDate = calCurrentWeekTime(false) + ' 23:59:59';
                query(PublicFunction.stringToDateTime(weekStartDate), PublicFunction.stringToDateTime(weekEndDate));
            } else if ($scope.month) {
                var monthStartDate = calCurrentMonthTime(true) + ' 00:00:00';
                var monthEndDate = calCurrentMonthTime(false) + ' 23:59:59';
                query(PublicFunction.stringToDateTime(monthStartDate), PublicFunction.stringToDateTime(monthEndDate));
            } else if ($scope.alltime) {
                query();
            } else if (!$scope.week && !$scope.month && !$scope.alltime) {
                PublicFunction.alert("请选择日期范围！", "查询有误");
            };

        };

        //初始化
        PublicModule.initModule($scope, 'Department');
        PublicModule.initModule($scope, 'User');
        PublicModule.initModule($scope, 'StationUser');

        $scope.Module.StationUser.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.StationUser.PageSize = 15;

        $scope.Module.StationUser.Populate = [{
            path: 'WorkLocation',
            select: 'Code Name'
        }, {
            path: 'User',
            select: 'Code Name Department',
            populate: {
                path: 'Department',
                select: 'Code Name',
                model: 'Department'
            }
        }];

        $scope.Module.User.Populate = {
            path: 'Department',
            select: 'Code Name'
        };

        //查询方法
        function query(starttime, endtime) {

            var searchText = {};
            searchText['Name'] = '/' + $scope.serachText + '/';
            searchText['Code'] = '/' + $scope.serachText + '/';

            //根据关键字模糊查询用户
            $scope.Module.User.find(searchText, null, null, function(records) {
                $scope.Module.User.Records = [];
                $scope.Module.User.Records = records;
                //根据关键字模糊查询部门
                var search = {};
                search['Name'] = $scope.serachText;
                search['Code'] = $scope.serachText;
                $scope.Module.Department.find(search, null, null, function(rcds) {
                    if (rcds.length > 0) { //找到部门，根据部门找用户
                        var deptObj = new Array();
                        for (var dept in rcds) {
                            deptObj.push(rcds[dept]._id);
                        }
                        var deptCon = {};
                        deptCon['$in'] = "/" + deptObj + "/";
                        $scope.Module.User.find({
                            "Department": deptCon
                        }, null, null, function(urcds) {
                            if (urcds.length > 0) { //根据部门找到用户
                                for (var user in urcds) {
                                    $scope.Module.User.Records.push(urcds[user]);
                                };
                                //根据用户找上下岗关联数据
                                var arrayObj = new Array();
                                for (var item in $scope.Module.User.Records) {
                                    arrayObj.push($scope.Module.User.Records[item]._id);
                                };
                                var tempCon = {};
                                tempCon['$in'] = arrayObj;

                                var condition = {};
                                condition['User'] = tempCon;
                                if (starttime == undefined) { //无时间

                                } else { //有时间
                                    condition['StartDateTime'] = {
                                        "$gte": starttime
                                    };
                                    condition['EndDateTime'] = {
                                        "$lte": endtime
                                    };
                                };
                                $scope.Module.StationUser.find(condition, null, null, function(rds) {
                                    $scope.Module.StationUser.Records = [];
                                    $scope.Module.StationUser.Records = rds;
                                    $scope.Module.StationUser.RecordCount = rds.length;
                                    $scope.Module.StationUser.firstPage();
                                });
                            } else { //根据部门未找到用户
                                var arrayObj = new Array();
                                for (var item in $scope.Module.User.Records) {
                                    arrayObj.push($scope.Module.User.Records[item]._id);
                                };
                                var tempCond = {};
                                tempCond['$in'] = arrayObj;

                                var condition = {};
                                condition['User'] = tempCon;
                                if (starttime == undefined) { //无时间

                                } else { //有时间
                                    condition['StartDateTime'] = {
                                        "$gte": starttime
                                    };
                                    condition['EndDateTime'] = {
                                        "$lte": endtime
                                    };
                                };
                                $scope.Module.StationUser.find(condition, null, null, function(rds) {
                                    $scope.Module.StationUser.Records = [];
                                    $scope.Module.StationUser.Records = rds;
                                    $scope.Module.StationUser.RecordCount = rds.length;
                                    $scope.Module.StationUser.firstPage();
                                });
                            };
                        });

                    } else { //未找到部门
                        if ($scope.Module.User.Records.length > 0) {
                            var arrayObj = new Array();
                            for (var item in $scope.Module.User.Records) {
                                arrayObj.push($scope.Module.User.Records[item]._id);
                            }
                            var tempCondition = {};
                            tempCondition['$in'] = arrayObj;

                            var condition = {};
                            condition['User'] = tempCon;
                            if (starttime == undefined) { //无时间

                            } else { //有时间
                                condition['StartDateTime'] = {
                                    "$gte": starttime
                                };
                                condition['EndDateTime'] = {
                                    "$lte": endtime
                                };
                            };
                            $scope.Module.StationUser.find(condition, null, null, function(rds) {
                                $scope.Module.StationUser.Records = [];
                                $scope.Module.StationUser.Records = rds;
                                $scope.Module.StationUser.RecordCount = rds.length;
                                $scope.Module.StationUser.firstPage();
                            });
                        } else {
                            $scope.Module.StationUser.Records = [];
                        };
                    }
                });
            });
        }

        //计算本周起止时间
        function calCurrentWeekTime(isStartTime) {
            var now = new Date(); //当前日期 
            var nowDayOfWeek = now.getDay(); //今天本周的第几天 
            var nowDay = now.getDate(); //当前日 
            var nowMonth = now.getMonth(); //当前月 
            var nowYear = now.getYear(); //当前年 
            nowYear += (nowYear < 2000) ? 1900 : 0; //
            var rtnTime;
            if (isStartTime) { //返回起始时间
                var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
                rtnTime = formatDate(weekStartDate);
            } else { //返回终止时间
                var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek + 1));
                rtnTime = formatDate(weekEndDate);
            };
            return rtnTime;
        }

        //计算本月起止时间
        function calCurrentMonthTime(isStartTime) {
            var now = new Date(); //当前日期 
            var nowMonth = now.getMonth(); //当前月 
            var nowYear = now.getYear(); //当前年 
            nowYear += (nowYear < 2000) ? 1900 : 0; //
            var rtnTime;
            if (isStartTime) { //返回起始时间
                var weekStartDate = new Date(nowYear, nowMonth, 1);
                rtnTime = formatDate(weekStartDate);
            } else { //返回终止时间
                //获取本月天数
                var monthStartDate = new Date(nowYear, nowMonth, 1);
                var monthEndDate = new Date(nowYear, nowMonth + 1, 1);
                var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);

                var weekEndDate = new Date(nowYear, nowMonth, days);
                rtnTime = formatDate(weekEndDate);
            };
            return rtnTime;
        }

        //格式化时间
        function formatDate(date) {
            var myyear = date.getFullYear();
            var mymonth = date.getMonth() + 1;
            var myweekday = date.getDate();
            return (myyear + "\\" + mymonth + "\\" + myweekday);
        }
    }
]);


//数据录入功能

// angular.module('Query').controller('PostsRecordController',
//     ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication', 'QCEnum',
//         function ($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication, QCEnum) {

//             $scope.btnSave = function () {

//                 $scope.Module.StationUser.save();
//             };
//             $scope.btnAdd = function () {
//                 $scope.Module.StationUser.add();
//             };
//             $scope.btnDelete = function () {
//                 $scope.Module.StationUser.delete();
//             };
//             $scope.btnFirstPage = function () {
//                 $scope.Module.StationUser.firstPage();
//             };
//             $scope.btnPrePage = function () {
//                 $scope.Module.StationUser.prePage();
//             };
//             $scope.btnNextPage = function () {
//                 $scope.Module.StationUser.nextPage();
//             };
//             $scope.btnLastPage = function () {
//                 $scope.Module.StationUser.lastPage();
//             };

//             //初始化
//             PublicModule.initModule($scope, 'StationUser');
//             $scope.Module.StationUser.Populate = [{path: 'WorkLocation', select: 'Code Name'},{path: 'User', select: 'Code Name'},{path: 'Terminal', select: 'Code Name'}];

//             if ($stateParams.id) {
//                 $scope.Module.StationUser.findById($stateParams.id, function (record) {
//                     $scope.Module.StationUser.FocusedRecord = record;
//                 })
//             }
//             else {
//                 $scope.btnAdd();
//             }
//             ;
//             $scope.Menu = '首页';
//         }
//     ]);